<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">
                <el-form-item>
                    <el-input v-model="query.keywords" placeholder="产品名字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select clearable v-model="query.state" placeholder="状态">
                        <el-option label="下架" value="0"></el-option>
                        <el-option label="上架" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" @click="handleAdd">新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="danger" @click="onSale" :disabled="this.sels.length===0">上架</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="warning" @click="downSale" :disabled="this.sels.length===0">下架</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表 -->
        <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="产品名字" width="150" >
            </el-table-column>
            <el-table-column prop="saleprice" label="售价" width="150" >
            </el-table-column>
            <el-table-column prop="costprice" label="成本" width="150" >
            </el-table-column>
            <el-table-column prop="salecount" label="销量" width="150" >
            </el-table-column>
            <el-table-column prop="state" label="状态" min-width="150" >
                <template scope="scope">
                    <span v-if="scope.row.state==0" style="color: #8c939d">下架</span>
                    <span v-if="scope.row.state==1" style="color: #13ce66">上架</span>
                </template>
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" width="200" >
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" width="200" >
            </el-table-column>
            <el-table-column label="操作" width="250">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                           :page-size="query.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="服务名称">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="成本价格" >
                    <el-input v-model="addForm.costprice" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="销售价格" >
                    <el-input v-model="addForm.saleprice" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="资源">
                    <el-upload
                            class="upload-demo"
                                action="/api/fastDfs/upload"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="简介" >
                    <quill-editor v-model="addForm.detail.intro" :options="quillConfig"></quill-editor>
                </el-form-item>
                <el-form-item label="预约须知" >
                    <quill-editor v-model="addForm.detail.orderNotice" :options="quillConfig"></quill-editor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>


    </section>
</template>

<script>
    import { quillEditor } from "vue-quill-editor" //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    /*导入 配置文件*/
    import quillConfig from '../../common/js/quill-config'

    export default {
    components:{//引入并加载组件
        quillEditor
    },
    data(){
        return {
            quillConfig:quillConfig,
            listLoading:false,//开启加载效果
            listData:[],
            fileList:[],
            sels:[],
            total:0, //数据的总条数
            query:{//分页条件
                pageSize:5,
                currentPage:1,
                keywords:'',
                state:null

            },
            addFormVisible:false, //默认隐藏弹出框
            addForm:{// 用于数据双向绑定对象
                name:'',
                resources:'',
                saleprice:0,
                costprice:0,
                state:0,
                salecount:0,
                detail:{
                    intro:'',
                    orderNotice:''
                }
            },
            formVisible:false,
            addFormRules: {
                name: [
                    { required: true, message: '请输入产品名称', trigger: 'blur' }
                ]
            },
            addLoading:false,

        }
    },
    methods:{
        //文件上传成功回调
        handleSuccess(response, file, fileList){
            console.debug(response);
            console.debug(fileList);
            // 多图上传
            let str = '';
            for (let i = 0;i<fileList.length;i++){
                // 判断是否是最后一个
                if (i == fileList.length - 1) {
                    str += fileList[i].response.resultObj;
                } else {
                    str += fileList[i].response.resultObj+",";
                }
            }
            this.addForm.resources = str;
        },
        //文件上传失败回调
        handlerError(err, file, fileList){

        },
        //文件删除
        handleRemove(file, fileList) {
            console.debug(file);
            console.debug(fileList);
            this.addForm.resources='';
        },
        // 编辑
        handleEdit(index,row){
            //数据回显
            this.addForm=Object.assign({}, row);
            // 先清空
            this.fileList = [];
            if (this.addForm.resources!=''){
                // 图片回显
                let arr = row.resources.split(",");
                for (let i = 0;i<arr.length;i++ ){
                    let obj = {
                        name: "图片" + (i+1),
                        url: "http://47.95.117.210:8888" + arr[i]
                    }
                    this.fileList.push(obj);
                }
            }
            //打开弹出框
            this.addFormVisible=true;

        },
        //给数据前面的选择按钮添加事件
        selsChange(val){
            //val 就是用户勾选的数据
            this.sels=val;
        },
        loadListData(){//远程请求列表数据
            this.$http.post("/pro",this.query).then(res=>{
                this.listData=res.data.data;
                this.total=res.data.total;
            })

        },
        // 上架
        onSale(){
            var ids = this.sels.map(item => item.id);
            this.$confirm('确认上架？', '提示', {
                type: 'warning'
            }).then(() => {
                this.listLoading = true;
                this.$http.patch("/pro/onSale",ids).then(res=>{
                    this.listLoading = false;
                    console.debug(res)
                    let {success,message}=res.data;
                    if (success){
                        this.$message({
                            message: '上架成功',
                            type: 'success'
                        });
                    }else {
                        this.$message.error('网络异常');
                    }
                    //重新加载数据
                    this.loadListData();
                })
            }).catch(() => {

            });
        },
        // 下架
        downSale(){
            var ids = this.sels.map(item => item.id);
            this.$confirm('确认下架？', '提示', {
                type: 'warning'
            }).then(() => {
                this.listLoading = true;
                this.$http.patch("/pro/downSale",ids).then(res=>{
                    this.listLoading = false;
                    console.debug(res)
                    let {success,message}=res.data;
                    if (success){
                        this.$message({
                            message: '下架成功',
                            type: 'success'
                        });
                    }else {
                        this.$message.error('网络异常');
                    }
                    //重新加载数据
                    this.loadListData();
                })
            }).catch(() => {

            });
        },
        //点击分页触发的方法
        handleCurrentChange(val){
            //val 就是点击翻页的时候 当前页页码
            this.query.currentPage=val;
            this.loadListData();
        },
        //查询的方法
        search(){
            //初始化开始的页码
           this.query.currentPage=1;
            //重新加载数据
           this.loadListData();
        },
        //新增的方法
        handleAdd(){
            //清空表单
            this.addForm={
                name:'',
                resources:'',
                saleprice:0,
                costprice:0,
                salecount:0,
                detail:{
                    intro:'',
                    orderNotice:''
                }
            }
            // 清空图片上传
            this.fileList=[]
            // 打开弹出框
            this.addFormVisible=true;
        },
        //删除的方法
        handleDel(index,row){
           //1.删除的确认框
            this.$confirm('确认删除该记录吗?', '提示', {
                type: 'warning'
            }).then(()=>{//确认删除的按钮
                this.listLoading = true;
                //获取删除数据id
                let id= row.id;
                //发送请求 "/dept/3"
                this.$http.delete("/pro/"+id).then(res=>{
                    this.listLoading = false;
                    console.debug(res)
                    let {success,message}=res.data;
                    if (success){
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                    }else {
                        this.$message.error('网络异常');
                    }
                    //重新加载数据
                    this.loadListData();
                })

            }).catch(()=>{//取消删除回调函数
                this.$message('已取消删除');
            })
        },
        addSubmit(){
           //开启加载效果
            this.addLoading=true;
            console.debug(this.addForm)
            //发送请求
            this.$http.put("/pro",this.addForm).then(res=>{
                console.debug(res)
                let {success,message}=res.data;
                if (success){
                    this.$message({
                        message: '操作成功',
                        type: 'success'
                    });
                }else {
                    this.$message({
                        message: '操作失败',
                        type: 'error'
                    });
                }
                //关闭加载效果
                this.addLoading=false;
                //关闭弹出框
                this.addFormVisible=false;
                //重新加载列表
                this.loadListData();
            })
        }

    },
    mounted(){
        //页面加载完毕以后立即执行此方法
        this.loadListData();
    }

}

</script>

<style>



</style>